<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="租户名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="租户名称"></el-input>
      </el-form-item>
      <el-form-item label="租户编号" prop="tenantNum">
        <el-input
          type="number"
          controls-position="right"
          :min="2"
          v-model="dataForm.tenantNum"
          placeholder="租户编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-date-picker
          v-model="dataForm.time"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="租户角色" prop="tenantRoleId">
        <el-select v-model="dataForm.tenantRoleId" placeholder="请选择">
          <el-option
            v-for="item in tenantRoleList"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <!--      <el-input v-model="dataForm.status" placeholder="状态（0：正常，1：冻结）"></el-input>-->
        <el-switch v-model="dataForm.status" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      tenantRoleList: [],
      dataForm: {
        time: '',
        id: 0,
        name: '',
        tenantNum: 2,
        startTime: '',
        endTime: '',
        status: true,
        createTime: '',
        updateTime: '',
        tenantRoleId: ''
      },
      dataRule: {
        name: [
          { required: true, message: '租户名称不能为空', trigger: 'blur' }
        ],
        tenantNum: [
          { required: true, message: '租户编号不能为空', trigger: 'blur' }
        ],
        time: [
          {
            required: true,
            message: '开始与结束时间不能为空',
            trigger: 'change'
          }
        ],
        status: [{ required: true, message: '状态', trigger: 'blur' }]
      }
    }
  },
  methods: {
    getAllTenantRoleList() {
      this.$http({
        url: '/tenant/role/select',
        method: 'post'
      }).then(res => {
        this.tenantRoleList = res.data
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      if (!this.dataForm.id) {
        this.getAllTenantRoleList()
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: '/tenant/info',
            method: 'post',
            data: { id: this.dataForm.id }
          }).then(data => {
            if (data && data.code === 200) {
              this.dataForm.name = data.data.name
              this.dataForm.tenantNum = data.data.tenantNum
              this.dataForm.startTime = data.data.startTime
              this.dataForm.endTime = data.data.endTime
              this.dataForm.time = [
                this.dataForm.startTime,
                this.dataForm.endTime
              ]
              this.dataForm.status = data.data.status == 1
              this.dataForm.tenantRoleId = data.data.tenantRoleId
              this.dataForm.createTime = data.data.createTime
              this.dataForm.updateTime = data.data.updateTime
              this.getAllTenantRoleList()
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: `/tenant/${
              !this.dataForm.id ? 'save' : 'update'
            }`,
            method: 'post',
            data: {
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              tenantNum: this.dataForm.tenantNum,
              startTime: this.dataForm.time[0],
              endTime: this.dataForm.time[1],
              status: this.dataForm.status ? 1 : 0,
              tenantRoleId: this.dataForm.tenantRoleId
            }
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
              this.$store.dispatch('user/fetchTenantList')
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
